package cn.edu.nuc.fourth.util;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import cn.edu.nuc.fourth.model.Major;
import cn.edu.nuc.fourth.model.School;
import cn.edu.nuc.fourth.model.SchoolMes;

public class ViewExcel extends AbstractXlsView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("list");
		sheet.setDefaultColumnWidth((short) 20);
		HSSFRow hssfRows = sheet.createRow(0);
		HSSFCell a = hssfRows.createCell(0);
		a.setCellValue("近五年办学条件报表");// 第一行

		HSSFRow hssfRow = sheet.createRow(1);
		HSSFCell hssfCell0 = hssfRow.createCell(0);
		hssfCell0.setCellValue("年份");
		HSSFCell hssfCell1 = hssfRow.createCell(1);
		hssfCell1.setCellValue("学生用餐座位数");
		HSSFCell hssfCell2 = hssfRow.createCell(2);
		hssfCell2.setCellValue("图书馆藏书数量");
		HSSFCell hssfCell3 = hssfRow.createCell(3);
		hssfCell3.setCellValue("报纸订阅数");
		HSSFCell hssfCell4 = hssfRow.createCell(4);
		hssfCell4.setCellValue("教师用台式机数量");
		HSSFCell hssfCell5 = hssfRow.createCell(5);
		hssfCell5.setCellValue("教师用笔记本数量");
		HSSFCell hssfCell6 = hssfRow.createCell(6);
		hssfCell6.setCellValue("学校自有产权建筑面积");
		HSSFCell hssfCell7 = hssfRow.createCell(7);
		hssfCell7.setCellValue("主校区自有产权建筑面积");
		HSSFCell hssfCell8 = hssfRow.createCell(8);
		hssfCell8.setCellValue("地方生均预算内拨款标准");
		HSSFCell hssfCell9 = hssfRow.createCell(9);
		hssfCell9.setCellValue("教学仪器设备总值");
		HSSFCell hssfCell10 = hssfRow.createCell(10);
		hssfCell10.setCellValue("负债总额");
		HSSFCell hssfCell11 = hssfRow.createCell(11);
		hssfCell11.setCellValue("货款总额");

		SchoolMes sm = (SchoolMes) model.get("school");
		List<School> schools = sm.getSchools();

		for (int i = 0; i < schools.size(); i++) {

			HSSFRow sheetRow = sheet.createRow(i + 2);
			School school = schools.get(i);
			String[] date = school.getSdate().split("-");
			sheetRow.createCell(0).setCellValue(date[0]);
			sheetRow.createCell(1).setCellValue(school.getEatSeats());
			sheetRow.createCell(1).setCellValue(school.getEatSeats());
			sheetRow.createCell(2).setCellValue(school.getlBooks());
			sheetRow.createCell(3).setCellValue(school.getNewspapers());
			sheetRow.createCell(4).setCellValue(school.gettComputers());
			sheetRow.createCell(5).setCellValue(school.gettNotebooks());
			sheetRow.createCell(6).setCellValue(school.getAllArea());
			sheetRow.createCell(7).setCellValue(school.getMainArea());
			sheetRow.createCell(8).setCellValue(school.getsStandard());
			sheetRow.createCell(9).setCellValue(school.getEquipments());
			sheetRow.createCell(10).setCellValue(school.getsMoney());
			sheetRow.createCell(11).setCellValue(school.getEquMoney());
		}

		HSSFRow majorRowss = sheet.createRow(schools.size() + 3);//创建行
		majorRowss.createCell(0).setCellValue("近五年专业与课程大类报表");
		HSSFRow majorRows = sheet.createRow(schools.size() + 4);
		HSSFCell majorfCell0 = majorRows.createCell(0);
		majorfCell0.setCellValue("年份");
		HSSFCell majorfCell1 = majorRows.createCell(1);
		majorfCell1.setCellValue("专业大类数");
		HSSFCell majorfCell2 = majorRows.createCell(2);
		majorfCell2.setCellValue("专业数");
		HSSFCell majorfCell3 = majorRows.createCell(3);
		majorfCell3.setCellValue("专业方向数");
		HSSFCell majorfCell4 = majorRows.createCell(4);
		majorfCell4.setCellValue("省部级以上示范重点专业数");
		HSSFCell majorfCell5 = majorRows.createCell(5);
		majorfCell5.setCellValue("省部级以上示范骨干专业数");
		HSSFCell majorfCell6 = majorRows.createCell(6);
		majorfCell6.setCellValue("省部级以上示范特色专业数");
		HSSFCell majorfCell7 = majorRows.createCell(7);
		majorfCell7.setCellValue("实现项目教学的专业课程数");
		HSSFCell majorfCell8 = majorRows.createCell(8);
		majorfCell8.setCellValue("校本教材数");
		HSSFCell majorfCell9 = majorRows.createCell(9);
		majorfCell9.setCellValue("教师主编并公开出版的教材数");
		HSSFCell majorfCell10 = majorRows.createCell(10);
		majorfCell10.setCellValue("牵头开发国家共建共享计划课程数");
		HSSFCell majorfCell11 = majorRows.createCell(11);
		majorfCell11.setCellValue("参与开发国家共建共享课程数");

		List<Major> majors = sm.getMajors();

		for (int i = 0; i < majors.size(); i++) {
			Major major = majors.get(i);
			HSSFRow majorRow = sheet.createRow(schools.size() + 5+i);
			
			String[] date = major.getMdate().split("-");
			majorRow.createCell(0).setCellValue(date[0]);
			majorRow.createCell(1).setCellValue(major.getMajors());
			majorRow.createCell(2).setCellValue(major.getMajors());
			majorRow.createCell(3).setCellValue(major.getMajors());
			majorRow.createCell(4).setCellValue(major.getMajors());
			majorRow.createCell(5).setCellValue(major.getMajors());
			majorRow.createCell(6).setCellValue(major.getMajors());
			majorRow.createCell(7).setCellValue(major.getMajors());
			majorRow.createCell(8).setCellValue(major.getMajors());
			majorRow.createCell(9).setCellValue(major.getMajors());
			majorRow.createCell(10).setCellValue(major.getMajors());
			majorRow.createCell(11).setCellValue(major.getMajors());
		}

		/*
		 * for (int i = 0; i < users.size(); i++) { HSSFRow sheetRow =
		 * sheet.createRow(i + 1); User user = users.get(i);
		 * sheetRow.createCell(0).setCellValue(user.getId());
		 * sheetRow.createCell(1).setCellValue(user.getUsername());
		 * sheetRow.createCell(2).setCellValue(user.getBirthday()); }
		 */

		String filename = new String(model.get("fileName").toString().getBytes("utf-8"), "iso-8859-1");// 设置下载时客户端Excel的名称
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename=" + filename);
		OutputStream ouputStream = response.getOutputStream();
		wb.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();

	}

	public <T> T getModel(Class<T> clazz, Map<String, Object> model) {

		Object obj = model.get("users");

		return (T) obj;
	}

}
